package servlet.UserManage;

import dao.UserDao;
import domain.Page;
import domain.User;
import util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;


public class UserInfo extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public UserInfo() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        int everyPage = 15;//每页记录数
        UserDao userdao=new UserDao();
        int totalCount = userdao.getCont();//获取总记录数
        //点击链接重新获取当前页
        String scurrentPage = request.getParameter("currentPage");
        int currentPage = 1; //当前页，默认1
        if(scurrentPage == null){
            currentPage = 1;//从第一页开始访问
        }else{
            currentPage = Integer.parseInt(scurrentPage);
        }
        //分页信息
        Page page = PageUtil.createPage(everyPage, totalCount, currentPage);
        //分页数据信息
        ArrayList<User> list = userdao.findByPage(page);
        PrintWriter out = response.getWriter();
        request.setAttribute("page", page);
        request.setAttribute("list", list);
        if(request.getAttribute("message")!=null) {
            request.setAttribute("message",request.getAttribute("message"));
        }
        request.getRequestDispatcher("/admin/userManage/index.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
